//
//  MTInviteInfoCell.swift
//  MT
//
//  Created by ooops on 2020/5/26.
//  Copyright © 2020 张强. All rights reserved.
//

//
//  MTFriendViewRequestVerifyCell.swift
//  MT
//
//  Created by ooops on 2020/5/6.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory
import Result

protocol MTInviteUserInfoCellDelegate: NSObjectProtocol {
    /// communication
    func communication(pushId: String)
}

class MTInviteUserInfoCell: UITableViewCell {
    
    /// MTProvider
    fileprivate lazy var taskProvider: MTProvider<MTCreateTaskAPI> = {
        return MTProvider<MTCreateTaskAPI>(plugins: [])
    }()
    
    var model: MTTaskInviteInfoSubModel? {
        didSet {
            userIconView.mtSetImage(model?.userHref, nil)
            userNickNameLabel.text = model?.nickname
            if model?.age != nil && model?.area != nil {
                descriptionLabel.text = "\(model?.age ?? 0)岁 | \(model?.area ?? "")"
            } else if model?.area == nil {
                descriptionLabel.text = "\(model?.age ?? 0)岁"
            } else {
                descriptionLabel.text = "\(model?.area ?? "")"
            }
            
            tagListView.snp.updateConstraints { (make) in
                make.height.equalTo(model?.tagList?.count ?? 0 > 0 ? 30 : 0)
            }
            tagListView.removeAllTags()
            tagListView.addTags(model?.tagList ?? [])
            
            //tipsLabel.text = model?.remark ?? "这家伙很懒, 什么都没留下"
            communitButton.isHidden = true
            atttImageView.isHidden = true
            if model?.roleType == 2 {
                if model?.joinStatus == 0 {
                    statusLabel.isHidden = false
                    statusLabel.text = "等待对方通过"
                    statusLabel.textColor = UIColor.text.x9b9b9b
                } else {
                    if model?.joinStatus == 1 {
                        statusLabel.isHidden = false
                        statusLabel.text = "对方拒绝了你的聚会申请"
                        statusLabel.textColor = UIColor.text.x9b9b9b
                    } else {
                        statusLabel.isHidden = false
                        atttImageView.isHidden = false
                        statusLabel.text = "恭喜你通过了报名"
                        statusLabel.textColor = UIColor(hex: 0xffc000)
                        communitButton.isHidden = false
                    }
                }
                
                if let vo = model?.partyTaskVo {
                    taskRemarkLabel.text = vo.remark
                    //giftIconView.mtSetImage(vo.giftHref, nil)
                    giftsLabel.attributedText = arrtibute(giftCount: vo.giftNum)
                    if let p = vo.inviteNum, let d = vo.startTime {
                        if let dateTime = DateManager.date(dateString: d, format: "yyyy-MM-dd HH:mm") {
                            descriptionLabel.text = "限\(p)人 / " + DateManager.string(date: dateTime, format: "MM月dd日 HH:mm")
                        }
                    }
                    addressLabel.text = vo.address
                }
                
            }
        }
    }
    
    private func arrtibute(giftCount: Int) -> NSAttributedString {
        
        let markAttribute = NSMutableAttributedString(string: "\(giftCount) / 人")
        let arrtibuteValue: [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : UIColor.text.x1d1a1a,
                                                             NSAttributedString.Key.font : PingFangSCSemiboldFont(24)]
        
        markAttribute.addAttributes(arrtibuteValue, range: NSRange(location: 0, length: "\(giftCount)".count))
        
        let xAttribute = NSMutableAttributedString(string: "x ")
        let xarrtibuteValue: [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : UIColor.text.x1d1a1a,
                                                               NSAttributedString.Key.font : PingFangSCSemiboldFont(18)]
        xAttribute.addAttributes(xarrtibuteValue, range: NSRange(location: 0, length: "x ".count))
        
        
        markAttribute.insert(xAttribute, at: 0)
        
        return markAttribute
    
    }
    
    weak var delegate: MTInviteUserInfoCellDelegate?
    
    lazy var userIconView: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.cornerRadius = 22
//        imageV.layer.borderWidth = 1
//        imageV.layer.borderColor = UIColor.black.cgColor
        imageV.layer.masksToBounds = true
        return imageV
    }()
    
    lazy var userNickNameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor.text.x1d1a1a
        //l.text = "帅气小伙子"
        return l
    }()
    
    lazy var descriptionLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "21岁   西湖区    8km"
        return l
    }()
    
    lazy var addressLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x9b9b9b
        l.text = "附加留言"
        return l
    }()
    
    lazy var taskRemarkLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x9b9b9b
        l.numberOfLines = 0
        l.preferredMaxLayoutWidth = ScreenWidth - 10 - 10 - 44 - 10
        l.lineBreakMode = .byWordWrapping
        l.text = "进群后看群通知。不要闲聊，根据要求提交证明截图。否则视为不符合条件，随时飞机票一张"
        return l
    }()
    
    lazy var communitButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("沟通群", for: .normal)
        btn.imageView?.tintColor = .blue
        btn.addTarget(self, action: #selector(commmunication), for: .touchUpInside)
        btn.setTitleColor(UIColor(hex: 0x29282b), for: .normal)
        btn.titleLabel?.font = PingFangSCMediumFont(14)
        btn.setTitleColor(UIColor.init(red: 66, green: 107, blue: 242), for: .normal)
        btn.isHidden = true
        return btn
    }()
    
    lazy var atttImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.image = UIImage(named: "icon_right_gary")?.byTintColor(UIColor(hex: 0x29282b))
        return imageV
    }()
    
    lazy var statusLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        l.textColor = UIColor.text.x9b9b9b
        l.textAlignment = .center
        l.isHidden = true
        return l
    }()
    
    lazy var giftIconView: UIImageView = {
        let imageV = UIImageView()
        imageV.contentMode = .scaleAspectFit
        imageV.clipsToBounds = true
        imageV.image = UIImage(named: "club_gift")
        return imageV
    }()
    
    lazy var giftsLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(24)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "x 33 / 人"
        return l
    }()
    
    @objc private func commmunication() {
        if let pushid = model?.pushId {
            self.delegate?.communication(pushId: "\(pushid)")
        }
    }
    
    lazy var centerLineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xf6f6f6)
        return v
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xf6f6f6)
        return v
    }()
    
    lazy var bottomLineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.init(red: 246, green: 246, blue: 246)
        return v
    }()
    
    lazy var tagListView: TagListView = {
        let v = TagListView()
        v.tagSelectedBackgroundColor = UIColor(hex: 0x426bf2)
        v.tagHighlightedBackgroundColor = UIColor(hex: 0x426bf2)
        v.tagBackgroundColor = UIColor(hex: 0x426bf2)
        v.textFont = PingFangSCMediumFont(10)
        v.textColor = UIColor.white
        v.cornerRadius = 8
        v.paddingX = 12
        v.paddingY = 4
        v.marginX = 10
        v.marginY = 10
//        v.borderColor = UIColor.text.x9b9b9b
//        v.borderWidth = 1
        //v.shadowColor = UIColor.black
        //v.shadowOffset = CGSize(width: 1, height: 1)
        //v.addTags(["不迟到", "有才艺", "会剪辑", "粉丝多", "表演能力强", "资金实力厚"])
        v.alignment = .left
        return v
    }()
    
    class func reuseIdentity() -> String {
        return "MTInviteUserInfoCellID"
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        makeConstraint()
        self.selectionStyle = .none
    }
    
    private func setupUI() {
        contentView.addSubview(userIconView)
        contentView.addSubview(userNickNameLabel)
        contentView.addSubview(descriptionLabel)
        
        contentView.addSubview(addressLabel)
        contentView.addSubview(giftIconView)
        contentView.addSubview(giftsLabel)
        
        contentView.addSubview(centerLineView)
        
        contentView.addSubview(taskRemarkLabel)
        
        contentView.addSubview(lineView)
        contentView.addSubview(statusLabel)
        contentView.addSubview(communitButton)
        contentView.addSubview(bottomLineView)
        contentView.addSubview(atttImageView)
        
        contentView.addSubview(tagListView)
        
    }
    
    private func makeConstraint() {
        
        userIconView.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview().offset(10)
            //make.size.equalTo(CGSize(width: 44, height: 44))
            make.width.equalTo(44)
            make.height.equalTo(44)
        }
        
        userNickNameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(userIconView.snp.right).offset(10)
            make.bottom.equalTo(userIconView.snp.centerY).offset(-2)
        }
        
        descriptionLabel.snp.makeConstraints { (make) in
            make.left.equalTo(userNickNameLabel)
            make.top.equalTo(userIconView.snp.centerY).offset(2)
        }
        
        addressLabel.snp.makeConstraints { (make) in
            make.left.equalTo(descriptionLabel)
            make.top.equalTo(descriptionLabel.snp.bottom).offset(4)
        }
        
        giftIconView.snp.makeConstraints { (make) in
            make.left.equalTo(addressLabel)
            make.top.equalTo(addressLabel.snp.bottom).offset(4)
            make.size.equalTo(CGSize(width: 42, height: 30))
        }
        
        giftsLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(giftIconView)
            make.left.equalTo(giftIconView.snp.right).offset(10)
        }
        
        centerLineView.snp.makeConstraints { (make) in
            make.left.equalTo(addressLabel)
            make.top.equalTo(giftIconView.snp.bottom).offset(3)
            make.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        taskRemarkLabel.snp.makeConstraints { (make) in
            make.left.equalTo(addressLabel)
            make.top.equalTo(centerLineView.snp.bottom).offset(4)
            make.right.equalToSuperview().offset(-16)
        }
        
        tagListView.snp.makeConstraints { (make) in
            make.left.right.equalTo(taskRemarkLabel)
            make.top.equalTo(taskRemarkLabel.snp.bottom).offset(10)
            make.height.equalTo(30)
        }
        
        statusLabel.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(lineView.snp.bottom)
            make.height.equalTo(36)
            make.bottom.equalTo(bottomLineView.snp.top)
        }
        
        communitButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(statusLabel)
            make.right.equalTo(atttImageView.snp.left)
        }
        
        atttImageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(statusLabel)
            make.right.equalToSuperview().offset(-10)
            make.size.equalTo(CGSize(width: 15, height: 15))
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(tagListView.snp.bottom).offset(20)
            make.height.equalTo(1)
        }
        
        bottomLineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(11)
        }
        
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


